package com.realife.eson.common.util.information;


import com.realife.eson.common.asserts.AssertNP;
import com.realife.eson.common.util.IdWorker;
import com.realife.eson.common.util.UniqueCodeUtil;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2022/5/30 21:06
 * @description:
 */
public class NamingEngine {
    private static IdWorker idWorker = new IdWorker(1, 1);

    private NamingEngine() {
    }


    public static String adjust(String name) {
        if (AssertNP.legalParams(name)) {
            return name.replaceAll("_", "").toLowerCase();
        }
        return null;
    }


    public static String getDeployUnitName(Integer appId, String logicAppName) {
        logicAppName.replaceAll("_", "");
        long id = idWorker.nextId();
        return (logicAppName + "-" + appId + "-" + id).toLowerCase();
    }

    /**
     * argocd不能用大写字母
     *
     * @param deploymentName
     * @param deployUnitName
     * @return
     */
    public static String getArgoAppName(String deploymentName, String deployUnitName) {
        String subName = "";
        if (deploymentName.length() <= 9) {
            subName = deploymentName;
        } else {
            int min = Math.min(deploymentName.length(), 9);
            subName = deploymentName.substring(0, min);
        }
        String result = subName + "-" + UniqueCodeUtil.getMD5(deployUnitName);
        result = result.replaceAll("_", "");
        if (result.length() <= 25) {
            return result.toLowerCase();
        }
        result = result.toLowerCase();
        return result.substring(0, 25);
    }

    public static String getHelmTemplateFileName() {
        return "";
    }


    public static void main(String[] args) {
        String argoAppName = getArgoAppName("SADFASFASDFDsdfgasdfasdfasdf", "asdfasdfas5465dfasdfasdfasdfasdfasdfasd");
        System.out.println("argoAppName =\n" + argoAppName);

        String mm = UniqueCodeUtil.getMD5("sadfg");
        System.out.println("mm =\n" + mm);
    }






}

